#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define BUF_SIZE 30
void error_handling( char * msg );

int main( int argc, char * argv[])
{
    int acpt_sock;
    int recv_sock;
    int str_len;
    int state; 
    struct sockaddr_in acpt_adr;
    struct sockaddr_in recv_adr;
    socklen_t recv_adr_sz;
    char buf[BUF_SIZE];

    if ( 2 != argc ) {
        printf(" Usage : %s <port> \n", argv[0] );
        exit( 1 );
    }

    acpt_sock = socket(PF_INET, SOCK_STREAM, 0 );
    memset( &acpt_adr, 0, sizeof(acpt_adr) );
    acpt_adr.sin_family = AF_INET;
    acpt_adr.sin_addr.s_addr = htonl( INADDR_ANY );
    acpt_adr.sin_port = htons( atoi( argv[1]) );

    if (-1 == bind(acpt_sock, (struct sockaddr *) &acpt_adr, sizeof(acpt_adr))) {
        error_handling( "bind() error");
    }
    listen( acpt_sock, 5 );

    recv_adr_sz = sizeof( recv_adr );
    recv_sock = accept( acpt_sock, (struct sockaddr *) &recv_adr, &recv_adr_sz);

    while (1)
    {
        str_len = recv( recv_sock, buf, sizeof(buf) - 1, MSG_PEEK | MSG_DONTWAIT);
        if ( str_len > 0) {
            break;
        }
    }

    buf[str_len] = 0;
    printf("Buffering %d bytes: %s \n", str_len, buf);

    str_len = recv( recv_sock, buf, sizeof( buf) - 1, 0);
    buf[ str_len ] = 0;
    printf(" Read again: %s \n", buf);
    close( acpt_sock );
    close( recv_sock );

    return 0;
}

void error_handling( char * msg )
{
    fputs( msg, stderr );
    fputc( '\n', stderr );
    exit ( 1 );
}